(defun square (x)
  (* x x))

(defun digits(n)
  (loop for i = n then (floor (/ i 10))
       while (> i 0)
       collect (mod i 10)))

(defun next-no-in-chain(n)
  (reduce #'+ (mapcar #'square (digits n))))

(defun next(n)
  (loop for i = n then (floor (/ i 10))
       while (> i 0)
       summing (square (mod i 10))))

(defun validp(n)
  (cond ((= n 1) nil)
	((= n 89) T)
	(T (validp (next n)))))

(defun probl92()
  (loop for i from 1 to 10000000 when (validp i) count i))